/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.common.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import merlion.crm.slcm.entity.Address;
import merlion.crm.slcm.entity.Customer;
import merlion.crm.slcm.entity.SalesLead;
import merlion.mrp.ppm.entity.SOP;
import merlion.mrp.ppm.entity.SalesForecast;

/**
 *
 * @author Zhang Ying nd Zhiqi
 */
@Entity
public class Staff implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    private String department;
    @OneToOne(targetEntity = DocFile.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private DocFile profilePicture;
    @OneToOne(targetEntity = Address.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private Address homeAddress = new Address();
    @OneToMany(mappedBy = "staff", targetEntity = Phone.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private Collection<Phone> phones = new ArrayList<Phone>();
    @OneToMany(targetEntity = SecurityRole.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Collection<SecurityRole> securityRoles = new ArrayList<SecurityRole>();
    @OneToOne(targetEntity = SystemUserAccount.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private SystemUserAccount systemUserAccount = new SystemUserAccount();
    @ManyToMany(targetEntity = SalesLead.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, mappedBy = "staffs")
    private Collection<SalesLead> salesLeads = new ArrayList<SalesLead>();
    
    // for mrp
    @OneToMany(mappedBy = "staff", targetEntity = SalesForecast.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Collection<SalesForecast> salesForecasts = new ArrayList<SalesForecast>();
    
    @OneToMany(mappedBy = "staff", targetEntity = SOP.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Collection<SOP> sOPs = new ArrayList<SOP>();
    
//    @OneToMany(mappedBy="staff", targetEntity=DailyMPS.class, cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
//    private Collection<DailyMPS> dailyMPSs = new ArrayList<DailyMPS>();

    public Staff() {
        systemUserAccount.setStaff(this);
        for (int i = 0; i < 4; i++) {
            Phone p = new Phone(i);
            p.setStaff(this);
            phones.add(p);
        }
    }

    public void create(String firstName, String lastName, String department) {
        this.setFirstName(firstName);
        this.setLastName(lastName);
        this.setDepartment(department);
    }
//
//    public Collection<DailyMPS> getDailyMPSs() {
//        return dailyMPSs;
//    }
//
//    public void setDailyMPSs(Collection<DailyMPS> dailyMPSs) {
//        this.dailyMPSs = dailyMPSs;
//    }

    public Collection<SOP> getsOPs() {
        return sOPs;
    }

    public void setsOPs(Collection<SOP> sOPs) {
        this.sOPs = sOPs;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getFullName() {
        return lastName.toUpperCase() + ", " + firstName;
    }

    public String getBizTel() {
        ArrayList<Phone> phonelist = new ArrayList<Phone>();
        phonelist.addAll(phones);
        return phonelist.get(0).getNumber();
    }

    public String getBizHp() {
        ArrayList<Phone> phonelist = new ArrayList<Phone>();
        phonelist.addAll(phones);
        return phonelist.get(1).getNumber();
    }

    public String getHomeTel() {
        ArrayList<Phone> phonelist = new ArrayList<Phone>();
        phonelist.addAll(phones);
        return phonelist.get(2).getNumber();
    }

    public String getHomeHp() {
        ArrayList<Phone> phonelist = new ArrayList<Phone>();
        phonelist.addAll(phones);
        return phonelist.get(3).getNumber();
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Collection<Phone> getPhones() {
        return phones;
    }

    public Collection<SecurityRole> getSecurityRoles() {
        return securityRoles;
    }

    public SystemUserAccount getSystemUserAccount() {
        return systemUserAccount;
    }

    public Long getId() {
        return id;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setPhones(Collection<Phone> phones) {
        this.phones = phones;
    }

    public void setSecurityRoles(Collection<SecurityRole> roles) {
        this.securityRoles = roles;
    }

    public void setSystemUserAccount(SystemUserAccount systemUserAccount) {
        this.systemUserAccount = systemUserAccount;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Address getHomeAddress() {
        return homeAddress;
    }

    public void setHomeAddress(Address homeAddress) {
        this.homeAddress = homeAddress;
    }

    public Collection<SalesLead> getSalesLeads() {
        return salesLeads;
    }

    public void setSalesLeads(Collection<SalesLead> salesLeads) {
        this.salesLeads = salesLeads;
    }

    public DocFile getProfilePicture() {
        return profilePicture;
    }

    public void setProfilePicture(DocFile profilePicture) {
        this.profilePicture = profilePicture;
    }

    public Collection<SalesForecast> getSalesForecasts() {
        return salesForecasts;
    }

    public void setSalesForecasts(Collection<SalesForecast> salesForecasts) {
        this.salesForecasts = salesForecasts;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Staff)) {
            return false;
        }
        Staff other = (Staff) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.common.entity.Staff[ id=" + id + " ]";
    }
}
